package com.mikao.util;

import com.mikao.util.PayCommonUtil;
import com.mikao.wxpay.WxPay;

import java.text.DecimalFormat;
import java.util.SortedMap;
import java.util.TreeMap;

public class WeChatUtil {
    /**
     * 基本常量设置
     */

    /**
     * APPID
     */
    public static String APP_ID = WxPay.appid;
    /**
     * 微信支付商户号
     */
    public static String MCH_ID= WxPay.mch_id;
    /**
     * 请求路径
     */
    public static String UFDODER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    /**
     * 密匙
     */
    public static String API_KEY = WxPay.secret;
    /**
     * 发起支付IP
     */
    public static String CREATE_IP = "112.117.94.77";
    /**
     * 回调url
     */
    public static String NOTIFY_URL = "http://47.106.79.235/videoshop_api-0.0.1-SNAPSHOT/prepay/wxPayNotify";

    /**
     * 生成微信签名
     * @param order_id
     *         订单ID
     * @param body
     *         描述
     * @param order_price
     *         价格
     * @return
     */
    public static  String GetWeChatXML(String order_id, String body, double order_price ,String spbill_create_ip){
        //随机字符串
        String nonce_str = WXPayUtil.generateNonceStr();
        // 回调接口
        String notify_url = WeChatUtil.NOTIFY_URL;
        //交易类型
        String trade_type = "APP";
        //微信价格最小单位分 转换为整数
        DecimalFormat df = new DecimalFormat("0");
        SortedMap<Object,Object> packageParams = new TreeMap<Object,Object>();
        packageParams.put("appid", APP_ID);
        packageParams.put("body", body);
        packageParams.put("mch_id", MCH_ID);
        packageParams.put("nonce_str", nonce_str);
        packageParams.put("notify_url", notify_url);
        packageParams.put("out_trade_no", order_id);
        packageParams.put("fee_type","CNY");
        packageParams.put("spbill_create_ip", spbill_create_ip);
        packageParams.put("total_fee", df.format(order_price*100).toString());
        packageParams.put("trade_type", trade_type);
        String sign = PayCommonUtil.createSign("UTF-8", packageParams,API_KEY);
        packageParams.put("sign", sign);
        String requestXML = PayCommonUtil.getRequestXml(packageParams);
/*        String xml = "<xml>" +
                "<appid>"+ APP_ID +"</appid>"+
                "<body>"+ body +"</body>"+
                "<mch_id>"+ MCH_ID +"</mch_id>"+
                "<nonce_str>"+ nonce_str +"</nonce_str>"+
                "<notify_url>"+ notify_url +"</notify_url>"+
                "<out_trade_no>"+ order_id +"</out_trade_no>"+
                "<spbill_create_ip>"+ spbill_create_ip +"</spbill_create_ip>"+
                "<total_fee>"+ price +"</total_fee>"+
                "<trade_type>APP</trade_type>"+
                "<sign>"+ sign +"</sign>"+
                "</xml>";
        System.out.println(xml);*/
        return requestXML;
    }

}
